<?php
namespace Company\Controller;
use Think\Controller;
class CategoryController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('category',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }

    public function index(){
        $this->display();
    }
    public function getCategory(){
        $list = $this->product->order("level,parent_id,sort")->select();
        $max_level= $this->product->field('max('.C('DB_PREFIX_T').'category.level) as max_level')->find();
        $list=$this->tree($list,$max_level['max_level']);
        if($list){
            alert(1,$list);
        }else{
            alert(0,'暂无');
        }
    }
    protected function tree($arr,$max){
        foreach($arr as $v){
            foreach($arr as $k=>$u){
                if($u['level']==$max && $u['parent_id']!==null){
                    $arr=$this->findParent($arr,$u['parent_id'],$k);
                }
            }
        }
        $max--;
        if($max>0){
            $arr=$this->tree($arr,$max);
        }
        return $arr;
    }
    protected function findParent($arr,$parent_id,$child_key){
        foreach($arr as $k=>$u) {
            if($u['id']==$parent_id){
                $arr[$k]['child'][]=$arr[$child_key];
                unset($arr[$child_key]);
                break;
            }
        }
        return $arr;
    }
    public function addD($name,$sort,$parent_id,$level){
        if($name==''){
            alert(0,'产品名称不能为空');
        }
        if(mb_strlen($name,'utf-8')>32){
            alert(0,'类目名称长度不能大于32');
        }
        if($level==''){
            $data['level']=1;
        }else{
            $data['level']=$level+1;
        }
        if($parent_id!=''){
            $data['parent_id']=$parent_id;
        }
        $name=trim($name);
        if($this->product->where("name='".$name."'")->count()>0){
            alert(0,'名称已经存在');
        }
        $data['name']=trim($name);
        $massage=session('company');
        $data['operator']=$massage['username'];
        $data['sort']=$sort;
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function editD($id,$name,$sort){
        $name=trim($name);
        if($name==''){
            alert(0,'产品名称不能为空');
        }
        if($id==''){
            alert(0,'操作有误');
        }
        if($this->product->where("name='".$name."' and id <>".$id)->count()>0){
            alert(0,'名称已经存在');
        }
        $data['name']=trim($name);
        $data['sort']=$sort;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        if($this->product->where("parent_id='".$id."'")->count()>0){
            alert(0,'该类目下存在子类目');
        }
        $result=$this->product->delete($id);
        if($result){
            alert(1,'删除成功');
        }else{
            alert(0,'删除失败');
        }
    }
}